<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="菜单名称">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入菜单名称" v-model="queryParam.name"></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator">
        <div></div>

        <a-button type="primary" @click="handleItem('edit', 0)" icon="plus">添加</a-button>
      </div>
      <a-table
        style="width: 100%; overflow-x: auto"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="false"
      >
        <span slot="name" slot-scope="text, record"> {{ record.parent_id ? '|____' : '' }} {{ text }} </span>

        <span slot="type" slot-scope="text">
          {{ getType(text) }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('edit', record)">编辑</a-button>
          <a-button type="link" size="small" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getMenuList, delMenu } from '@/api/setting/wechatConfig'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '名称',
    dataIndex: 'name',
    scopedSlots: {
      customRender: 'name',
    },
  },
  {
    title: '类型',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'type',
    },
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    width: 120,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 150,
  },
]

export default {
  data() {
    return {
      columns,
      queryParam: {
        name: '',
      },
      list: [],
      selectedRowKeys: [],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getType() {
      return (type) => {
        let text
        switch (type) {
          case 0:
            text = '链接'
            break

          case 1:
            text = '关键字'
            break

          case 2:
            text = '小程序'
            break

          case 3:
            text = '图文'
            break
        }

        return text
      }
    },
  },

  methods: {
    async getList() {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam } = this

      const res = await getMenuList({
        ...queryParam,
      })

      if (!res.code) {
        this.list = res.data.list || []
      }

      hide()
    },

    // 点击单个
    handleItem(type, item) {
      switch (type) {
        case 'edit':
          this.edit(item)
          break

        case 'remove':
          this.remove({
            id: item.id,
          })
          break
      }
    },

    edit(item) {
      let path = '/saasChannel/wechatConfigMenuEidt'
      if (item) {
        path += `?id=${item.id}`
      }

      this.$router.push({
        path,
      })
    },

    remove(data) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: async () => {
          const hide = this.$message.loading('请稍等...')
          const res = await delMenu(data)
          hide()
          if (res.code) {
            this.$error({
              content: res.msg,
            })
            return
          }
          this.$message.success(res.msg)
          this.getList()
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

// .flex {
//   .inp {
//     margin-right: 16px;
//   }
// }

.table-operator {
  display: flex;
  justify-content: space-between;
}
</style>
